﻿
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<link href="~/plugins/zTree_v3/css/bootstrapStyle/bootstrapStyle.css" rel="stylesheet" />
<div class="ibox">
    <div class="ibox-content">
        <div class="row">
            <div class="col-md-2">
                <div class="ibox">
                    <div class="ibox-title">栏目列表</div>
                    <div class="ibox-content">
                        <ul id="channelTree" class="ztree"></ul>
                    </div>
                </div>
                
            </div>
            <div class="col-md-10">
                <div id="toolbar" class="btn-group">

                </div>
                <table id="table"></table>
            </div>
        </div>

    </div>
</div>
@section scripts{
    <script src="~/plugins/zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script>
        var $table, channelTree, $dicTable, channelId = 0,lastChannelNode;
        $(function () {
            loadChannelTree();
            loadGrid();
        });
        function loadChannelTree() {
            var opts = {

                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "pId",
                    }
                }, callback: {
                    onClick: DicZtreeClick

                },check: {
                    enable: true,
                    chkStyle: "radio"
                }
            };
            jutils.ajaxGet('/Cms/Channel/GetChannelTree', null, function (res) {
                channelTree = $.fn.zTree.init($("#channelTree"), opts, res.data.channels);
                channelTree.expandAll(true);
            });
        }

        //单击事件
        function DicZtreeClick(event, treeId, treeNode) {
            if (lastChannelNode) {
                channelTree.checkNode(lastChannelNode, false, true);
            }
            channelTree.checkNode(treeNode, true, true);
            lastChannelNode = treeNode;
            //console.log(treeNode);
            channelId = treeNode.id;
            $table.bootstrapTable('refresh');

        }
        function loadGrid() {
            $table = $("#table").bootstrapTable({
                url: '/cms/content/GetData',
                toolbar: '#toolbar',
                pagination: true,
                height: $(window).height() - 50,
                pageSize: 15,
                pageList: [5, 10, 15, 20],
                showColumns: true,
                showRefresh: true,
                clickToSelect: true,
                sidePagination: "server",//
                locale: "zh-CN",
                striped: true,
                uniqueId: 'id',
                queryParams: function (params) {
                    var dto = {
                        limit: params.limit, //每页多少条
                        offset: params.offset, //从第几条开始读(默认就好)
                        channelId: channelId
                    };
                    return dto;
                },
                columns: [
                    { field: 'ck', checkbox: true },
                    { field: 'id', title: '文章ID', align: 'left'  },
                    { field: 'title', title: '文章标题', align: 'left',formatter:renderTitle },
                    { field: 'insert_time', title: '发布时间', align: 'center' },
                    {
                        field: 'status', title: '状态', align: 'center', formatter: function (v) {
                            if (v === 1) {
                                return '<span class="label label-primary">已审核</span>';
                            }

                            return '<span class="label">待审核</span>';
                        }
                    },
                    {
                        field: 'id', title: '操作', align: 'center',
                        formatter: render
                    }
                ]

            });
            //jutils.initToolBar('#toolbar', true);
            jutils.initToolBarRole('#toolbar');
        }

        function render(v, r, i) {
            var html = jutils.initToolBarRow(v);
            return html;
        }

        function renderTitle(v, d) {
            var thtml = '<a href="/news/' + d.channel_id + '/' + d.id + '" target="_blank">' + v +'&nbsp;&nbsp;';
            if (d.is_top==1) {
                thtml += '<img src="/static/images/top.gif"/>&nbsp;&nbsp;';
            }
            if (d.hit_count > 100) {
                thtml += '<img src="/static/images/hot.gif"/>&nbsp;&nbsp;';
            }
            if (d.is_recommend) {
                thtml += '<img src="/static/images/tuijian.png"/>&nbsp;&nbsp;';
            }
            thtml += '</a>';
            return thtml;

            //return '<a href="/static/content/' + d.channel_id + '/' + d.id + '.html" target="_blank">' + v + '</a>';
        }
        function add() {
            if (channelId == '0') {
                layer.alert("请先选择栏目");
                return;
            }
            var url = '/cms/Content/Form?id=0&channelId=' + channelId;
            parent.$.altasTab.open(
                {
                    id: 'edit_content',
                    title: '发布文章',
                    url: url,
                    close: function () {
                        $table.bootstrapTable('refresh');
                    }
                });

        }
        function edit(obj) {
            //if (channelId == '0') {
            //    layer.alert("请先选择栏目");
            //    return;
            //}
            var id = $(obj).data('id');
            var row = $table.bootstrapTable('getRowByUniqueId', id);
            var channel = row.channel_id;
          
            var url = '/cms/Content/Form?id=' + id + '&channelId=' + channel;
            parent.$.altasTab.open(
                {
                    id: 'edit_content',
                    title: '修改文章',
                    url: url,
                    close: function () {
                        $table.bootstrapTable('refresh');
                    }
                });
        }

        function del(obj) {
            var id = $(obj).data('id');
            jutils.confirm("确认删除当前文章吗", function () {
                jutils.ajaxGet('/cms/Content/DeleteById', { id: id }, function (res) {
                    $table.bootstrapTable('refresh');
                })
            })
        }


        function setTop() {
            var rows = $table.bootstrapTable('getSelections');

            if (rows.length == 0) {
                jutils.error("请选择需要置顶的文章数据行");
                return;
            }
            var ids = [];
            for (var i = 0; i < rows.length; i++) {
                ids.push(rows[i].id);
            }
            jutils.confirm("确认置顶选择的文章吗？", function () {
                jutils.ajaxGet('/cms/content/SetTop', { ids: ids }, function (res) {
                    $table.bootstrapTable('refresh');
                });
            });
        }

        function setRecomend() {
            var rows = $table.bootstrapTable('getSelections');
            if (rows.length == 0) {
                jutils.error("请选择需要推荐的文章数据行");
                return;
            }
            var ids = [];
            for (var i = 0; i < rows.length; i++) {
                ids.push(rows[i].id);
            }
            jutils.confirm("确认置顶选择的文章吗？", function () {
                jutils.ajaxGet('/cms/content/SetRecomend', { ids: ids }, function (res) {
                    $table.bootstrapTable('refresh');
                });
            });

        }
    </script>
}





